package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import uteis.Constantes;

import beans.ItensdeCobranca;
import beans.ServicoLocalizarItens;
import model.Servico;

public class ServicoJDBCCadastro implements ServicoCadastroDAO {

	Connection conexao;
	private String cdformapagto;
	private String cdregistro;
	private String cdinstcobranca;
	private String cdtpservico;
	private ServicoLocalizarItens locitens = new ServicoLocalizarItens();

	public ServicoJDBCCadastro(Connection conexao) {
		this.conexao = conexao;
	}

	public String salvar(Servico servico) {
		String sqlquery = "insert into SERVICO (CDTPSERVICO, CDREGISTRO, CDFORMAPAGTO, CDINSTCOBRANCA, USUINCLUSAO, COMPINCLUSAO, USUALTERACAO, COMPALTERACAO, DTINCLUSAO, "
				+ "DTALTERACAO, BOLDML, CDEMPRESA)"
				+ " values(?,?,?,?,?,?,?,?,?,?,?,?);";

		PreparedStatement pstmt;
		try {

			cdregistro = locitens.getCdregistro();
			cdformapagto = locitens.getCdformapagto();
			cdinstcobranca = locitens.getCdinstcobranca();
			cdtpservico = locitens.getCdtpservico();

			pstmt = this.conexao.prepareStatement(sqlquery);
			pstmt.setString(1, cdtpservico);
			pstmt.setString(2, cdregistro);
			pstmt.setString(3, cdformapagto);
			pstmt.setString(4, cdinstcobranca);
			pstmt.setString(5, "PABLO");
			pstmt.setString(6, "PABLO");
			pstmt.setString(7, "PABLO");
			pstmt.setString(8, "PABLO");
			pstmt.setDate(9, null);
			// pstmt.setTime(8, new
			// java.sql.Time(formapagto.getHrinclusao().getTime()));
			pstmt.setDate(10, null);
			// pstmt.setTime(10, new
			// java.sql.Time(formapagto.getHralteracao().getTime()));
			pstmt.setString(11, "I");
			pstmt.setString(12, Constantes.cdempresas);
			pstmt.execute();

			return ("success");

		} catch (Exception e) {
			e.printStackTrace();
			return ("error");
		}
	}

	public boolean ResultSevico(String nmtpservico) {
		/**
		 * String sqlquery =
		 * "SELECT CDSERVICO FROM SERVICO WHERE CDEMPRESA = "+Constantes
		 * .cdempresas+
		 * " AND CDINSTCOBRANCA = "+ItensdeCobranca.scdinstcobranca+
		 * " AND CDTPSERVICO = " +nmtpservico+" ORDER BY CDSERVICO ASC;";
		 **/
		String sqlquery = "SELECT S.CDSERVICO CDSERVICO, TP.NMTPSERVICO NMTPSERVICO FROM "
				+ " SERVICO S, TPSERVICO TP WHERE S.CDEMPRESA = "
				+ Constantes.cdempresas
				+ " AND S.CDINSTCOBRANCA = "
				+ ItensdeCobranca.scdinstcobranca
				+ " AND S.CDTPSERVICO = "
				+ nmtpservico + " AND(S.CDTPSERVICO = TP.CDTPSERVICO);";

		try {
			// String valor = "";
			java.sql.Statement stmt = conexao.createStatement();
			ResultSet rs = null;
			rs = stmt.executeQuery(sqlquery);
			while (rs.next()) {
				// valor = rs.getString("CDSERVICO");
				ItensdeCobranca.scdtpservico = rs.getString("CDSERVICO");
				ItensdeCobranca.snmtpservico = rs.getString("NMTPSERVICO");
			}
			if (ItensdeCobranca.scdtpservico.equals("")) {
				return (false);
			} else {
				return (true);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return (false);
		}
	}

	public void editar(Servico servico) {
	}

	public void excluir(Servico servico) {
	}

	public List<Servico> ListarServicoT() {
		return null;
	}

	public Connection getConexao() {
		return conexao;
	}

	public void setConexao(Connection conexao) {
		this.conexao = conexao;
	}

	public String getCdformapagto() {
		return cdformapagto;
	}

	public void setCdformapagto(String cdformapagto) {
		this.cdformapagto = cdformapagto;
	}

	public String getCdregistro() {
		return cdregistro;
	}

	public void setCdregistro(String cdregistro) {
		this.cdregistro = cdregistro;
	}

	public String getCdinstcobranca() {
		return cdinstcobranca;
	}

	public void setCdinstcobranca(String cdinstcobranca) {
		this.cdinstcobranca = cdinstcobranca;
	}

	public String getCdtpservico() {
		return cdtpservico;
	}

	public void setCdtpservico(String cdtpservico) {
		this.cdtpservico = cdtpservico;
	}

}
